<template>
  <div class="userLoginView">
    <h2>用户登录</h2>
    <a-form
      :model="form"
      @submit="handleSubmit"
      auto-label-width
      style="max-width: 480px; margin: 0 auto"
    >
      <a-form-item field="userAccount" tooltip="请输入账号" label="账号">
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item field="userPassword" label="密码">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码,密码不少于8位"
        />
      </a-form-item>
      <a-form-item>
        <a-button
          html-type="register"
          style="width: 120px; margin-right: 200px"
          @click="userRegister"
          >注册
        </a-button>
        <a-button type="primary" html-type="submit" style="width: 120px"
          >登录
        </a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script setup lang="ts">
import { reactive } from "vue";
import { UserControllerService } from "../../../generated";
import message from "@arco-design/web-vue/es/message";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

/**
 * 表单信息
 */
const form = reactive({
  userAccount: "",
  userPassword: "",
});
/**
 * 提交登录
 * @param data
 */
const router = useRouter();
const store = useStore();
const handleSubmit = async () => {
  const res = await UserControllerService.userLoginUsingPost(form);
  if (res.code === 0) {
    // 跳转首页
    await store.dispatch("user/getLoginUser");
    router.push({
      path: "/",
      replace: true,
    });
    // 登陆成功后执行全局状态管理
  } else {
    message.error("登陆失败," + res.message);
  }
};

const userRegister = async () => {
  await router.push({
    path: "/user/register",
  });
};
</script>
